home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / help_ai / if < prev    next >
Text File  |  1995-02-18  |  2KB  |  88 lines

  1. if:
  2.  
  3.     1) Introduction ------------------------------------------------
  4.  
  5.     The if statement is very similar to the C if statement. 
  6.  
  7.         if ( test-expression )
  8.         {
  9.           statement(s)
  10.         else
  11.           statement(s)
  12.         }
  13.  
  14.     The "else" is optional, and the braces are always required.
  15.     The differences between the C-language if-statement and RLaB's
  16.     are due to the special demands of an interactive language.
  17.  
  18.     Examples:
  19.  
  20.         //-----------------------------------
  21.  
  22.         if ( init ) 
  23.             {
  24.           mass = 10.0;
  25.           inertia = 3*mass/length^3;
  26.           init = 0;
  27.         }
  28.  
  29.         //-----------------------------------
  30.  
  31.         if(class(data) == "string") 
  32.         {
  33.           fprintf(file, data);
  34.         else
  35.           write(file, data);
  36.         }
  37.  
  38.         //-----------------------------------
  39.  
  40.         if( class(v) != "matrix" ) { error(); }
  41.  
  42.         //-----------------------------------
  43.  
  44.     At present there is no explicit elseif statement. However the
  45.     else if behavior can be implemented as it is in C (although
  46.     RLaB's syntax is clumsier). For example:
  47.  
  48.         if( test1 ) 
  49.         {
  50.           x = a;
  51.         else if( test2 ) {
  52.           x = b;
  53.         else if( test3 ) {
  54.           x = c;
  55.         else 
  56.           error();
  57.         }}}
  58.  
  59.     2) Finer Points  -----------------------------------------------
  60.  
  61.     The if statement requires that the test-expression be a scalar
  62.     quantity. If it is necessary to test a matrix, the result of
  63.     the matrix test must be reduced to a scalar quantity. The
  64.     any(), all() and find() functions are useful in this
  65.     situation.
  66.  
  67.     Example:
  68.  
  69.         > a = [1,2,3;4,5,6;7,8,9];
  70.         > a < 100
  71.                 1          1          1  
  72.                 1          1          1  
  73.                 1          1          1  
  74.         > all (a < 100)
  75.                 1          1          1  
  76.         > all (all (a < 100))
  77.                 1  
  78.  
  79.     In the above example, the result of a matrix test was first
  80.     reduced to a vector result with one use of all(). A second use
  81.     of all() reduces the vector result to a scalar result, which
  82.     is now suitable for inclusion in an if statement
  83.     test-expression.
  84.  
  85.         > if (all (all (a < 100))) { "all a < 100" }
  86.         all a < 100
  87.  
  88.